package com.example.w_library.http.download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 下载工具类
 */
public class DownloadUtil {


    /**
     * 下载
     * @param url 路径
     */
    public static byte[] downloadFile(String url){

        byte[] bytes = new byte[1024];

        try {
            File file = new File("");

            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

            InputStream is =  connection.getInputStream();

            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());

            while  (is.read(bytes) != -1){
                fos.write(bytes);
            }

            fos.close();

            is.close();

            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bytes;
    }
}
